<template>
	<view class="home-page">
		<u-navbar title="首页" bgColor="rgba(255,255,255,0.0)" left-icon="" :placeholder="true">
		</u-navbar>
		<view>
			<u-swiper radius="15" height="300rpx" keyName="url" :list="swiperlist"></u-swiper>
		</view>
		<view class="box">
			<view class="box-title">
				<view class="title-left">
					<image src="../../static/home/fire.png" mode="widthFix"></image>
					<text>爆款区</text>
				</view>
				<!-- <view class="title-right">
					<text>查看更多</text>
					<image src="../../static/home/right.png" mode=""></image>
				</view> -->
			</view>
			<view class="hot">
				<view class="hot-item" v-for="(item,index) in hotlist" :key="index" @click="goDetails(item.id)">
					<image :src="item.goodsImgs" mode=""></image>
					<view class="hot-info">
						<view class="hot-title">
							{{item.goodsName}}
						</view>
						<view class="hot-price">
							￥{{item.currentPrice}}
						</view>
						<view class="hot-buy">
							去批货
						</view>
					</view>
				</view>
			</view>
		</view>
		<view class="box">
			<view class="box-title">
				<view class="title-left">
					<image src="../../static/home/good.png" mode="widthFix"></image>
					<text>推荐商品</text>
				</view>
				<!-- 	<view class="title-right">
					<text>查看更多</text>
					<image src="../../static/home/right.png" mode=""></image>
				</view> -->
			</view>
			<view class="goods">
				<view class="goods-item" v-for="(item,index) in relist" :key="index" @click="goDetails(item.id)">
					<image :src="item.goodsImgs" mode=""></image>
					<view class="goods-right">
						<view class="goods-title">
							{{item.goodsName}}
						</view>
						<view class="goods-num">
							已售出{{item.quantitySold}}
						</view>
						<view class="goods-price">
							￥<text>{{item.currentPrice}}</text>
						</view>
					</view>
				</view>
			</view>
		</view>
		<u-loadmore :status="status" />
	</view>
</template>

<script>
	export default {
		data() {
			return {
				hotlist: [],
				relist: [],
				swiperlist: [],
				page: 1,
				status: 'loadmore',
			}

		},
		onLoad() {
			this.posthotlist()
			this.postrelist()
			this.getswiper()
		},
		onShow() {
			this.playNewOrderSound();
		},
		onReachBottom() {
			this.page = this.page + 1
			this.status = 'loading';
			this.postrelist()
		},
		methods: {
			playNewOrderSound() {
				const audio = uni.createInnerAudioContext(); // 创建音频上下文
				// 设置音频文件的路径（假设您已上传到服务器）
				audio.src = '/static/tisp.mp3';
				// 播放音频
				audio.play();
				// 监听音频播放结束
				audio.onPlay(() => {
					console.log('音频开始播放');
				});
				audio.onEnded(() => {
					console.log('音频播放完毕');
					audio.destroy(); // 销毁音频对象，避免内存泄漏
				});
			},
			getswiper() {
				this.$Request({
					method: 'GET',
					url: '/api/goods/banner/list',
					data: {},
					ismsg: false,
				}).then(res => {
					if (res.code == 200) {
						this.swiperlist = res.data
					}
				})
			},
			postrelist() {
				let params = {
					pageNum: this.page,
					pageSize: 10
				}
				this.$Request({
					method: 'GET',
					url: '/api/goods/info/re-list',
					data: params,
					ismsg: false,
				}).then(res => {
					console.log(res)
					if (res.code == 200) {
						if (res.rows.length < 10) {
							this.status = 'nomore';
						}
						if (this.page == 1) {
							this.relist = res.rows
						} else {
							this.relist = [...this.relist, ...res.rows]
						}
					}
				})
			},
			posthotlist() {
				let params = {
					pageNum: 1,
					pageSize: 3
				}
				this.$Request({
					method: 'GET',
					url: '/api/goods/info/hot-list',
					data: params,
					ismsg: false,
				}).then(res => {
					if (res.code == 200) {
						this.hotlist = res.rows
					}
				})
			},
			goDetails(id) {
				uni.navigateTo({
					url: '/pages/index/details/index?id=' + id
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.home-page {
		min-height: calc(100vh - 91rpx);
		background-image: url('../../static/goods/top-bg.png');
		background-size: 100% 561rpx;
		background-repeat: no-repeat;
		padding: 30rpx;

		.box {
			padding: 30rpx;
			margin-top: 30rpx;
			border-radius: 30rpx;
			background-color: #ffffff;
			box-shadow: 0rpx 0rpx 16rpx 1rpx rgba(0, 0, 0, 0.08);

			.box-title {
				display: flex;
				justify-content: space-between;
				align-items: center;
				color: #333333;

				.title-left {
					display: flex;
					align-items: center;
					font-size: 32rpx;
					font-weight: bold;

					image {
						width: 30rpx;
						height: 30rpx;
						margin-right: 10rpx;
					}
				}

				.title-right {
					font-size: 24rpx;

					image {
						width: 17rpx;
						height: 20rpx;
						margin-left: 10rpx;
					}
				}
			}

			.hot {
				margin-top: 20rpx;
				display: flex;

				.hot-item {
					width: 31%;
					background-color: #F6F6F6;
					border-radius: 20rpx;
					overflow: hidden;
					margin-right: 3.1%;

					image {
						width: 100%;
						height: 200rpx;
					}

					.hot-info {
						padding: 20rpx;
						color: #333333;

						.hot-title {
							overflow: hidden;
							white-space: nowrap;
							text-overflow: ellipsis;
							font-size: 26rpx;
							font-weight: bold;
						}

						.hot-price {
							margin-top: 10rpx;
							font-size: 24rpx;
						}

						.hot-buy {
							text-align: center;
							margin-top: 15rpx;
							font-size: 24rpx;
							color: #ffffff;
							padding: 10rpx 40rpx;
							border-radius: 30rpx;
							background: linear-gradient(90deg, #FE8A03 0%, #FA5900 100%);
						}
					}

				}

				.hot-item:last-child {
					margin-right: 0;
				}
			}

			.goods {

				.goods-item {
					display: flex;
					align-items: center;
					margin-top: 30rpx;

					image {
						width: 232rpx;
						height: 232rpx;
						border-radius: 30rpx;
					}

					.goods-right {
						margin-left: 20rpx;

						.goods-title {
							color: #333333;
							font-size: 28rpx;
							font-weight: bold;
						}

						.goods-num {
							font-size: 24rpx;
							color: #999999;
							margin-top: 10rpx;
						}

						.goods-price {
							width: 388rpx;
							height: 60rpx;
							background-image: url('../../static/home/price-bg.png');
							background-repeat: no-repeat;
							background-size: 100% 100%;
							line-height: 60rpx;
							display: flex;
							align-items: center;
							color: #FB510D;
							padding: 0 15rpx;
							margin-top: 50rpx;

							text {
								font-size: 48rpx;
								font-weight: bold;
								margin-left: 10rpx;
							}
						}
					}
				}
			}

		}
	}
</style>